﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
using YArchitech.Electric;
using YArchitech.MEP.Electric.Common;

namespace HYElectricSetting
{
	public partial class WireSizeForm : System.Windows.Forms.Form, IElectricSettingItem
	{
		public WireSizeForm(Document doc)
		{
			this.InitializeComponent();
			this.doc = doc;
			this.isEdited = false;
		}

		private void WireSizeForm_Load(object sender, EventArgs e)
		{
			this.InitButtonImage();
			this.PrepareSettingData();
			this.InitWireTypeCombox();
		}

		public bool IsEdited
		{
			get
			{
				return this.isEdited;
			}
			set
			{
				this.isEdited = value;
				HYElectricSettingMainForm hyelectricSettingMainForm = base.Tag as HYElectricSettingMainForm;
				bool flag = this.isEdited && hyelectricSettingMainForm != null;
				if (flag)
				{
					hyelectricSettingMainForm.ValidApplyButton();
				}
			}
		}

		public void SaveElectricSettingData()
		{
			ElectricSettingData.SaveWireSettingData(this.doc);
		}

		private void InitButtonImage()
		{
			this.buttonNewSize.Image = Resources.GetImage("Add20");
			this.buttonEditSize.Image = Resources.GetImage("ReName20");
			this.buttonDelSize.Image = Resources.GetImage("Delete20");
			ToolTip toolTip = new ToolTip();
			toolTip.InitialDelay = 500;
			toolTip.AutoPopDelay = 3000;
			toolTip.ReshowDelay = 50;
			toolTip.ShowAlways = true;
			toolTip.SetToolTip(this.buttonNewSize, "添加");
			toolTip.SetToolTip(this.buttonEditSize, "编辑");
			toolTip.SetToolTip(this.buttonDelSize, "删除");
		}

		private void PrepareSettingData()
		{
			ElectricSettingData.InitWireSettingProcessData(this.doc);
			bool flag = ElectricSettingData.WireSettingProcessData.Count <= 0;
			if (flag)
			{
				ElectricSettingData.WireSettingProcessData = ElectricSettingData.ReadDefaultWireSettingData(this.doc);
				this.IsEdited = true;
			}
		}

		private void InitWireTypeCombox()
		{
			this.cmbWireType.Items.Clear();
			foreach (WireSizeData item in ElectricSettingData.WireSettingProcessData)
			{
				this.cmbWireType.Items.Add(item);
			}
			bool flag = this.cmbWireType.Items.Count > 0;
			if (flag)
			{
				this.cmbWireType.SelectedIndex = 0;
			}
		}

		private void UpdateDataGridView()
		{
			WireSizeData wireSizeData = (WireSizeData)this.cmbWireType.SelectedItem;
			bool flag = wireSizeData == null;
			if (!flag)
			{
				this.dataGridView.Rows.Clear();
				foreach (KeyValuePair<double, double> keyValuePair in wireSizeData.WireSizes)
				{
					DataGridViewRow dataGridViewRow = new DataGridViewRow();
					int index = this.dataGridView.Rows.Add(dataGridViewRow);
					dataGridViewRow = this.dataGridView.Rows[index];
					dataGridViewRow.Cells[0].Value = keyValuePair.Key;
					dataGridViewRow.Cells[1].Value = keyValuePair.Value;
				}
			}
		}

		private void SelectDataGridViewRow(double key)
		{
			foreach (object obj in ((IEnumerable)this.dataGridView.Rows))
			{
				DataGridViewRow dataGridViewRow = (DataGridViewRow)obj;
				double d = (double)dataGridViewRow.Cells[0].Value;
				bool flag = MathUtils.IsEqual(key, d, 0.1);
				if (flag)
				{
					dataGridViewRow.Selected = true;
					return;
				}
			}
			bool flag2 = this.dataGridView.Rows.Count > 0;
			if (flag2)
			{
				this.dataGridView.Rows[0].Selected = true;
			}
		}

		private void cmbWireType_SelectedIndexChanged(object sender, EventArgs e)
		{
			this.UpdateDataGridView();
		}

		private void buttonAdd_Click(object sender, EventArgs e)
		{
			bool flag = this.cmbWireType.Items.Count <= 0;
			if (!flag)
			{
				List<string> list = new List<string>();
				for (int i = 0; i < this.cmbWireType.Items.Count; i++)
				{
					WireSizeData wireSizeData = (WireSizeData)this.cmbWireType.Items[i];
					list.Add(wireSizeData.ToString());
				}
				CreateItemForm createItemForm = new CreateItemForm("新建导线类型", list);
				bool flag2 = createItemForm.ShowDialog() == DialogResult.OK;
				if (flag2)
				{
					WireSizeData wireSizeData2 = this.FindItem(createItemForm.BaseName);
					bool flag3 = wireSizeData2 != null;
					if (flag3)
					{
						WireSizeData wireSizeData3 = new WireSizeData(wireSizeData2);
						wireSizeData3.WireType.Name = createItemForm.NewName;
						ElectricSettingData.WireSettingProcessData.Add(wireSizeData3);
						this.InitWireTypeCombox();
						this.cmbWireType.SelectedItem = wireSizeData3;
						this.IsEdited = true;
					}
				}
			}
		}

		private void buttonDel_Click(object sender, EventArgs e)
		{
			bool flag = this.cmbWireType.Items.Count == 1;
			if (flag)
			{
				TaskDialog.Show("盈建科提示", "不能删除最后的导线类型。");
			}
			else
			{
				int selectedIndex = this.cmbWireType.SelectedIndex;
				bool flag2 = selectedIndex < 0 || MessageBox.Show("确定要删除选中项吗？", "盈建科提示", MessageBoxButtons.OKCancel) != DialogResult.OK;
				if (!flag2)
				{
					WireSizeData item = (WireSizeData)this.cmbWireType.SelectedItem;
					ElectricSettingData.WireSettingProcessData.Remove(item);
					this.InitWireTypeCombox();
					bool flag3 = selectedIndex < this.cmbWireType.Items.Count;
					if (flag3)
					{
						this.cmbWireType.SelectedIndex = selectedIndex;
					}
					this.IsEdited = true;
				}
			}
		}

		private void buttonNewSize_Click(object sender, EventArgs e)
		{
			WireSizeData wireSizeData = (WireSizeData)this.cmbWireType.SelectedItem;
			bool flag = wireSizeData == null;
			if (!flag)
			{
				List<string> list = new List<string>();
				foreach (KeyValuePair<double, double> keyValuePair in wireSizeData.WireSizes)
				{
					list.Add(keyValuePair.Key.ToString());
				}
				CreateWireSizeForm createWireSizeForm = new CreateWireSizeForm("新建", string.Empty, string.Empty, list);
				bool flag2 = createWireSizeForm.ShowDialog() == DialogResult.OK;
				if (flag2)
				{
					double key = Convert.ToDouble(createWireSizeForm.Section);
					double value = Convert.ToDouble(createWireSizeForm.Ampacity);
					bool flag3 = wireSizeData.WireSizes.ContainsKey(key);
					if (!flag3)
					{
						wireSizeData.WireSizes.Add(key, value);
						wireSizeData.WireSizeSorted();
						this.UpdateDataGridView();
						this.SelectDataGridViewRow(key);
						this.IsEdited = true;
					}
				}
			}
		}

		private void buttonEditSize_Click(object sender, EventArgs e)
		{
			WireSizeData wireSizeData = (WireSizeData)this.cmbWireType.SelectedItem;
			bool flag = wireSizeData == null;
			if (!flag)
			{
				bool flag2 = this.dataGridView.SelectedRows.Count <= 0;
				if (!flag2)
				{
					DataGridViewRow dataGridViewRow = this.dataGridView.SelectedRows[0];
					bool flag3 = dataGridViewRow == null;
					if (!flag3)
					{
						double num = (double)dataGridViewRow.Cells[0].Value;
						double num2 = (double)dataGridViewRow.Cells[1].Value;
						List<string> list = new List<string>();
						foreach (KeyValuePair<double, double> keyValuePair in wireSizeData.WireSizes)
						{
							list.Add(keyValuePair.Key.ToString());
						}
						CreateWireSizeForm createWireSizeForm = new CreateWireSizeForm("编辑", num.ToString(), num2.ToString(), list);
						bool flag4 = createWireSizeForm.ShowDialog() == DialogResult.OK;
						if (flag4)
						{
							double num3 = Convert.ToDouble(createWireSizeForm.Section);
							double value = Convert.ToDouble(createWireSizeForm.Ampacity);
							bool flag5 = MathUtils.IsEqual(num, num3, 0.1);
							if (flag5)
							{
								wireSizeData.WireSizes[num] = value;
							}
							else
							{
								bool flag6 = wireSizeData.WireSizes.ContainsKey(num3);
								if (flag6)
								{
									return;
								}
								wireSizeData.WireSizes.Remove(num);
								wireSizeData.WireSizes.Add(num3, value);
								wireSizeData.WireSizeSorted();
							}
							this.UpdateDataGridView();
							this.SelectDataGridViewRow(num3);
							this.IsEdited = true;
						}
					}
				}
			}
		}

		private void buttonDelSize_Click(object sender, EventArgs e)
		{
			WireSizeData wireSizeData = (WireSizeData)this.cmbWireType.SelectedItem;
			bool flag = wireSizeData == null;
			if (!flag)
			{
				bool flag2 = this.dataGridView.SelectedRows.Count <= 0;
				if (!flag2)
				{
					DataGridViewRow dataGridViewRow = this.dataGridView.SelectedRows[0];
					bool flag3 = dataGridViewRow == null;
					if (!flag3)
					{
						int index = dataGridViewRow.Index;
						double key = (double)dataGridViewRow.Cells[0].Value;
						wireSizeData.WireSizes.Remove(key);
						this.UpdateDataGridView();
						bool flag4 = index < this.dataGridView.Rows.Count;
						if (flag4)
						{
							this.dataGridView.Rows[index].Selected = true;
						}
						this.IsEdited = true;
					}
				}
			}
		}

		private WireSizeData FindItem(string wireTypeName)
		{
			return ElectricSettingData.WireSettingProcessData.Find((WireSizeData item) => item.WireType.Name == wireTypeName);
		}

		private Document doc = null;

		private bool isEdited = false;
	}
}
